import { requestClient } from '#/api/request';

export interface RoleInfo {
  id: number;
  name: string;
  code: string;
  description?: string;
  attr?: string;
  createTime?: string;
  updateTime?: string;
}

export interface RoleListResult {
  total: number;
  items: RoleInfo[];
}

export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

export namespace RoleApi {
  export interface PageFetchParams {
    page: number;
    pageSize: number;
    search?: string;
  }

  export interface SaveParams {
    id?: number;
    name: string;
    code: string;
    description?: string;
  }

  export interface role {
    id: number;
    name: string;
    desc: string;
  }
}

/**
 * 获取角色信息
 */
export async function getRolesApi(params: RoleApi.PageFetchParams) {
  // 直接传递参数，不要嵌套在 params 对象中
  return requestClient.post('/auth/role/list', params);
}

/**
 * 获取系统角色列表
 */
export async function getSysRoles() {
  return requestClient.get<{ items: RoleInfo[]; total: number }>(
    '/auth/role/system-roles',
  );
}

export async function getRolesNoSuperApi(params: RoleApi.PageFetchParams) {
  return requestClient.get('/auth/role/no-super-list', { params });
}

export interface RoleSaveDto {
  role: {
    attr?: string;
    description?: string;
    id?: number;
    name: string;
  };
  permissionIds: number[];
}

export function saveRole(data: RoleSaveDto) {
  return requestClient.post('/auth/role/save', data);
}

export async function deleteRoleById(id: string) {
  return requestClient.delete<RoleApi.role>(`/auth/role/delete?id=${id}`);
}

// 获取权限树（支持新增和编辑）
export function getPermissionTree(roleId?: number | string) {
  const params = roleId ? { roleId } : {};
  return requestClient.get<any>('/auth/role/permission-tree', { params });
}
